package pcjavanet.mobilesell.service;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;  
import java.util.Iterator; 
import java.util.Map;      
import pcjavanet.mobilesell.model.Employee;
import pcjavanet.mobilesell.model.EmployeeExample;
import pcjavanet.mobilesell.model.Eventcatalog;
import pcjavanet.mobilesell.model.EventcatalogExample;
import pcjavanet.mobilesell.model.Event; 
import pcjavanet.mobilesell.model.PageBean;
import pcjavanet.mobilesell.model.EventExample;
import pcjavanet.mobilesell.model.QryCondition;
import pcjavanet.mobilesell.model.QryTerm;
import pcjavanet.mobilesell.persistence.EmployeeMapper;
import pcjavanet.mobilesell.persistence.EventcatalogMapper;
import pcjavanet.mobilesell.persistence.EventMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class EventService {


  @Autowired
  private  EventMapper eventMapper;
  @Autowired
  private  EmployeeMapper employeeMapper;
  @Autowired
  private  EventcatalogMapper eventcatalogMapper;


  public int  insert( Event event ) throws Exception{
	return eventMapper.insert (event);
  }


  public Event  selectByPrimaryKey( Event  bean ) {
 	int dbid = bean.getDbid();
	Event event = eventMapper.selectByPrimaryKey( dbid );
	Employee employee = employeeMapper.selectByPrimaryKey( event.getRefEmployeeDbid() );
	event.setEmployee(employee) ;
	Eventcatalog eventcatalog = eventcatalogMapper.selectByPrimaryKey( event.getRefEventcatalogDbid() );
	event.setEventcatalog(eventcatalog) ;
	return event ;
  }


  public int  updateByPrimaryKeySelective( Event event ) throws Exception{
	return eventMapper.updateByPrimaryKeySelective( event );
  }


  public int  deleteByPrimaryKey( Event  bean ) throws Exception{
 	int dbid = bean.getDbid();
	return eventMapper.deleteByPrimaryKey( dbid );
  }


  public List<Event>  selectByExample( Event event ) throws Exception{
    EventExample example = new EventExample();
	return eventMapper.selectByExample( example );
  }

  public List<Event>  getOnePageBeans( QryCondition condition , PageBean pageBean ) throws Exception{
    EventExample example = new EventExample();
    ServiceUtil.dealCriteria( condition,  example.createCriteria() );
	List<Event> ls=eventMapper.getOnePageBeans( example , pageBean );
//begin
	if ( ls.size() > 0 ) {
  		Map<Integer,Integer> employeeIdmap = new HashMap<Integer,Integer>();
  		for(Event  m : ls){
  			m.setEmployee(new Employee ()) ; 
  			if ( m.getRefEmployeeDbid() != null ) 
  				employeeIdmap.put(m.getRefEmployeeDbid(), m.getRefEmployeeDbid());
  		};
  		List<Integer> refEmployeeDbids = new ArrayList<Integer>();
  		Iterator<Integer> refEmployeeDbidit = employeeIdmap.keySet().iterator(); 
  		while ( refEmployeeDbidit.hasNext() ){
  			refEmployeeDbids.add( refEmployeeDbidit.next() );
  		}
  		EmployeeExample employeeExample = new EmployeeExample();
  		if ( refEmployeeDbids.size() > 0 ) 
  			employeeExample.createCriteria().andDbidIn( refEmployeeDbids );
  		List<Employee> employees = employeeMapper.selectByExample( employeeExample);
  		for(Event m : ls ){
  			for(Employee c : employees ){
  				if ( m.getRefEmployeeDbid() !=null && c.getDbid() !=null && m.getRefEmployeeDbid().intValue() == c.getDbid().intValue() ) {
  					m.setEmployee( c );
  				}
  			}
  		}
  		Map<Integer,Integer> eventcatalogIdmap = new HashMap<Integer,Integer>();
  		for(Event  m : ls){
  			m.setEventcatalog(new Eventcatalog ()) ; 
  			if ( m.getRefEventcatalogDbid() != null ) 
  				eventcatalogIdmap.put(m.getRefEventcatalogDbid(), m.getRefEventcatalogDbid());
  		};
  		List<Integer> refEventcatalogDbids = new ArrayList<Integer>();
  		Iterator<Integer> refEventcatalogDbidit = eventcatalogIdmap.keySet().iterator(); 
  		while ( refEventcatalogDbidit.hasNext() ){
  			refEventcatalogDbids.add( refEventcatalogDbidit.next() );
  		}
  		EventcatalogExample eventcatalogExample = new EventcatalogExample();
  		if ( refEventcatalogDbids.size() > 0 ) 
  			eventcatalogExample.createCriteria().andDbidIn( refEventcatalogDbids );
  		List<Eventcatalog> eventcatalogs = eventcatalogMapper.selectByExample( eventcatalogExample);
  		for(Event m : ls ){
  			for(Eventcatalog c : eventcatalogs ){
  				if ( m.getRefEventcatalogDbid() !=null && c.getDbid() !=null && m.getRefEventcatalogDbid().intValue() == c.getDbid().intValue() ) {
  					m.setEventcatalog( c );
  				}
  			}
  		}
	}
//end
	return ls;
  }

  public int getTotalRecords( QryCondition condition , PageBean pageBean ) throws Exception{
    EventExample example = new EventExample();
    ServiceUtil.dealCriteria( condition,  example.createCriteria() );
	return eventMapper.getTotalRecords( example , pageBean );
  }

  public int countByExample( Event event , PageBean pageBean ){
    EventExample example = new EventExample();
	return eventMapper.countByExample( example );
  }
  public List<Event> selectByExamplePage( Event event , PageBean pageBean ){
  EventExample example = new EventExample();
	return eventMapper.selectByExamplePage( example , pageBean );
  }

}
